package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.UserService;
import com.dongdongshop.util.ShiroUtils;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("userController")
@Controller
public class UserController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Reference
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("getMessage")
    @ResponseBody
    public Result getMessage(String phone){
        if (!StringUtils.isBlank(phone)){
            rocketMQTemplate.convertAndSend("message",phone);
            return Result.build(ConstantEnum.SUCCESS);
        }

        return Result.build(ConstantEnum.ERROR);
    }

    @RequestMapping("register")
    @ResponseBody
    public Result register(TbUser tbUser,String code){
        String salt = ShiroUtils.generateSalt(6);
        String md5 = ShiroUtils.encryptPassword("MD5", tbUser.getPassword(), salt, 3);

        tbUser.setSalt(salt);
        tbUser.setPassword(md5);
        userService.insert(tbUser,code);

        return Result.build(ConstantEnum.SUCCESS);

    }
}
